package com.eedi.framework.logger.dal.mapper;

import com.eedi.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.logger.controller.org.vo.OrgOperateLogPageReq;
import com.eedi.framework.logger.controller.org.vo.OrgOperateLogResp;
import com.eedi.framework.logger.controller.sys.vo.SysOrgOperateLogPageReq;
import com.eedi.framework.logger.convert.OrgOperateLogConvert;
import com.eedi.framework.logger.dal.dataobject.OrgLogOperateDO;
import com.eedi.framework.logger.enmus.SysLogTypeEnum;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.org.core.db.OrgInfoBaseDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;

@Mapper
public interface OrgLogOperateMapper extends BaseMapperX<OrgLogOperateDO> {


    default PageResult<OrgOperateLogResp> selectPage(OrgOperateLogPageReq reqVO, Collection<String> userIds, SysLogTypeEnum sysLogType,String orgInfoId) {
        LambdaQueryWrapperX<OrgLogOperateDO> query = new LambdaQueryWrapperX<OrgLogOperateDO>()
                .likeIfPresent(OrgLogOperateDO::getModule, reqVO.getModule())
                .inIfPresent(OrgLogOperateDO::getUserId, userIds)
                .eqIfPresent(OrgLogOperateDO::getOptType, reqVO.getOptType())
                .eqIfPresent(OrgLogOperateDO::getUserType, reqVO.getUserType())
                .eqIfPresent(OrgInfoBaseDO::getOrgInfoId,orgInfoId)
                .betweenIfPresent(OrgLogOperateDO::getStartTime, reqVO.getStartTime(), reqVO.getEndTime());
        if (Boolean.TRUE.equals(reqVO.getSuccess())) {
            query.eq(OrgLogOperateDO::getResultCode, GlobalErrorCodeConstants.SUCCESS.getCode());
        } else if (Boolean.FALSE.equals(reqVO.getSuccess())) {
            query.gt(OrgLogOperateDO::getResultCode, GlobalErrorCodeConstants.SUCCESS.getCode());
        }
        // 降序
        query.orderByDesc(OrgLogOperateDO::getSysOperateLogId);
        PageResult<OrgLogOperateDO> orgLogOperates = selectPage(reqVO, query);
        return orgLogOperates.convertPage(OrgOperateLogConvert.INSTANCE::convert);
    }

    default PageResult<OrgOperateLogResp> selectPage(SysOrgOperateLogPageReq reqVO, Collection<String> userIds, SysLogTypeEnum sysLogType, String tenantId) {
        LambdaQueryWrapperX<OrgLogOperateDO> query = new LambdaQueryWrapperX<OrgLogOperateDO>()
                .likeIfPresent(OrgLogOperateDO::getModule, reqVO.getModule())
                .inIfPresent(OrgLogOperateDO::getUserId, userIds)
                .eqIfPresent(OrgInfoBaseDO::getOrgInfoId,reqVO.getOrgInfoId())
                .eqIfPresent(OrgLogOperateDO::getOptType, reqVO.getOptType())
                .eqIfPresent(OrgLogOperateDO::getUserType, reqVO.getUserType())
                .eqIfPresent(OrgInfoBaseDO::getSysTenantId,tenantId)
                .betweenIfPresent(OrgLogOperateDO::getStartTime, reqVO.getStartTime(), reqVO.getEndTime());
        if (Boolean.TRUE.equals(reqVO.getSuccess())) {
            query.eq(OrgLogOperateDO::getResultCode, GlobalErrorCodeConstants.SUCCESS.getCode());
        } else if (Boolean.FALSE.equals(reqVO.getSuccess())) {
            query.gt(OrgLogOperateDO::getResultCode, GlobalErrorCodeConstants.SUCCESS.getCode());
        }
        // 降序
        query.orderByDesc(OrgLogOperateDO::getSysOperateLogId);
        PageResult<OrgLogOperateDO> orgLogOperates = selectPage(reqVO, query);
        return orgLogOperates.convertPage(OrgOperateLogConvert.INSTANCE::convert);
    }
}
